package com.cyz.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.cyz.exception.SessionIsExistException;
import com.cyz.pojo.Paging;
import com.cyz.service.SessionService;
import com.cyz.service.TypeService;
import com.google.gson.Gson;

@Controller
@RequestMapping("/SessionManage")
public class SessionManage {

	@Resource(name="mv")
	private ModelAndView mv;
	@Resource(name="gson")
	private Gson gson;
	@Resource(name="typeServiceImpl")
	private TypeService typeService;
	@Resource(name="sessionServiceImpl")
	private SessionService sessionService;
	
	/**
	 * 场次列表页面 
	 * @return
	 */
	@RequestMapping("/session-list")
	public ModelAndView sessionList(HttpServletRequest request,HttpServletResponse response) {
		mv.addObject("typeList",typeService.getAllType());
		mv.setViewName("admin/reserve-list");
		mv.setViewName("admin/session-list");
		return mv;
	}
	
	/**
	 * 添加场次页面 
	 * @return
	 */
	@RequestMapping("/session-add")
	public ModelAndView sessionAdd(HttpServletRequest request,HttpServletResponse response) {
		mv.addObject("typeList",typeService.getAllType());
		mv.setViewName("admin/reserve-list");
		mv.setViewName("admin/session-add");
		return mv;
	}
	
	/**
	 * 取消场次列表页面 
	 * @return
	 */
	@RequestMapping("/session-cancel")
	public ModelAndView sessionCancel(HttpServletRequest request,HttpServletResponse response) {
		mv.addObject("typeList",typeService.getAllType());
		mv.setViewName("admin/reserve-list");
		mv.setViewName("admin/session-cancel");
		return mv;
	}
	
	/**
	 * 已经过期的场次列表页面 
	 * @return
	 */
	@RequestMapping("/session-expired")
	public ModelAndView sessionExpired(HttpServletRequest request,HttpServletResponse response) {
		mv.addObject("typeList",typeService.getAllType());
		mv.setViewName("admin/reserve-list");
		mv.setViewName("admin/session-expired");
		return mv;
	}
	
	/**
	 * 获取场次
	 * @throws IOException 
	 */
	@RequestMapping("/getSession")
	public void getSession(HttpServletRequest request,HttpServletResponse response) throws IOException {
		response.setContentType("text/xml;charset=UTF-8");
	    response.setCharacterEncoding("UTF-8");
		PrintWriter printWriter = response.getWriter();
		Map map = new HashMap();
		map.put("page",Integer.parseInt(request.getParameter("page")));
		map.put("pageSize",Integer.parseInt(request.getParameter("pageSize")));
		map.put("typeName",request.getParameter("typeName"));
		map.put("subject",request.getParameter("subject"));
		map.put("startTime",request.getParameter("startTime"));
		map.put("endTime",request.getParameter("endTime"));
		map.put("state",Integer.parseInt(request.getParameter("state")));
		Paging paging = sessionService.getSession(map);
		String s = gson.toJson(paging);
		printWriter.write(s);
		printWriter.close();	
	}
	
	/**
	 * 添加场次
	 * @throws IOException 
	 */
	@RequestMapping("/addSession")
	public void addSession(HttpServletRequest request,HttpServletResponse response) throws IOException {
		PrintWriter printWriter = response.getWriter();
		
		String place = request.getParameter("place"); //场地
		String typeName = request.getParameter("typeName"); //考试类型名称
		String subject = request.getParameter("subject"); //科目
		String times = request.getParameter("times"); //时间段
		String dates = request.getParameter("dates"); //日期
		Integer remain = Integer.parseInt(request.getParameter("remain"));
		try {
			sessionService.addSession(typeName, dates, times, remain, place, subject);
			printWriter.write("{\"res\":\"1\"}");
		} catch (SessionIsExistException e) { //场次已存在，添加失败
			printWriter.write("{\"res\":\"0\"}");
		}
		printWriter.close();
	}
	
	/**
	 * 批量取消预约
	 * @throws IOException 
	 */
	@RequestMapping("/deleteSessionByQuery")
	public void deleteReserveByQuery(HttpServletRequest request,HttpServletResponse response) throws IOException {
		PrintWriter printWriter = response.getWriter();
		String list = request.getParameter("sessionIdList");
		String[] list1 = gson.fromJson(list,String[].class); //获取要删除的学生编号
		
		for(String sessionId:list1) { //循环遍历删除
			sessionService.deleteSession(Integer.parseInt(sessionId));
		}
		printWriter.write("{\"res\":\"1\"}");
		printWriter.close();
	}
	
	/**
	 * 删除预约
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/deleteSession")
	public void deleteReserve(HttpServletRequest request,HttpServletResponse response) throws IOException {
		PrintWriter printWriter = response.getWriter();
		Integer sessionId = Integer.parseInt(request.getParameter("sessionId"));
		sessionService.deleteSession(sessionId);
		printWriter.write("{\"res\":\"1\"}");
		printWriter.close();
	}
	
}
